﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using HostpitalDevice.Business;

namespace HostpitalDevice
{
    public partial class ExportForm : Form
    {
        private  int       companyId;

        /// <summary>
        /// 需要导出的员工的身份证集合 
        /// </summary>
        private  ArrayList m_Idcards;
        public ArrayList IdCards
        {
            get { return m_Idcards; }
            set { m_Idcards = value; }
        }

        public ExportForm(string _company)
        {
            companyId =CompanyManage.GetCompanyIdByName ( _company);
            m_Idcards = new ArrayList();
            InitializeComponent();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            if (this.checkAllExport.Checked == true && this.checkOptimeExport.Checked == true)
            {
                MessageBox.Show("两者不能同时选择，只能选择其一");
                checkedListEmployee.Items.Clear();
                return;
            }
            if (this.checkAllExport.Checked == true && this.checkOptimeExport.Checked == false)
            //如果是全部导出
            {
                string temp;
                checkedListEmployee.Items.Clear();
                IDataReader reader1 = EmployeeInfoCon.getAllEmployeeInfoByCompanyId(companyId);
                if (reader1!=null )
                {
                    while (reader1.Read())
                    {
                        temp = "";
                        if (!Convert.IsDBNull(reader1["employeename"]))
                            temp = reader1["employeename"].ToString().Trim();
                        if (!Convert.IsDBNull(reader1["idcard"]))
                            temp += "-" + reader1["idcard"].ToString().Trim();
                        checkedListEmployee.Items.Add(temp, true);
                    }
                }
                IDataReader reader2 = EmployeeInfoCon.getAllTempEmployeeInfoByCompanyId(companyId);
                if (reader2!=null )
                {
                    while (reader2.Read())
                    {
                        temp = "";
                        if (!Convert.IsDBNull(reader2["employeename"]))
                            temp = reader2["employeename"].ToString().Trim();
                        if (!Convert.IsDBNull(reader2["idcard"]))
                            temp += "-" + reader2["idcard"].ToString().Trim();
                        checkedListEmployee.Items.Add(temp, true);
                    }
                }
                return;
            }
            if (this.checkAllExport.Checked == false && this.checkOptimeExport.Checked == true)
            //如果是按照时间段导出
            {
                string temp;
                checkedListEmployee.Items.Clear();
                
                IDataReader reader1 = EmployeeInfoCon.getAllEmployeeInfoByOptime(this.pickerBeginOptime.Value,this.pickerEndOptime.Value );
                if (reader1 != null)
                {
                    while (reader1.Read())
                    {
                        temp = "";
                        if (!Convert.IsDBNull(reader1["employeename"]))
                            temp = reader1["employeename"].ToString().Trim();
                        if (!Convert.IsDBNull(reader1["idcard"]))
                            temp += "-" + reader1["idcard"].ToString().Trim();
                        checkedListEmployee.Items.Add(temp, true);
                    }
                }
                IDataReader reader2 = EmployeeInfoCon.getAllTempEmployeeInfoByOptime(this.pickerBeginOptime.Value ,this.pickerEndOptime.Value );
                if (reader2 != null)
                {
                    while (reader2.Read())
                    {
                        temp = "";
                        if (!Convert.IsDBNull(reader2["employeename"]))
                            temp = reader2["employeename"].ToString().Trim();
                        if (!Convert.IsDBNull(reader2["idcard"]))
                            temp += "-" + reader2["idcard"].ToString().Trim();
                        checkedListEmployee.Items.Add(temp, true);
                    }
                }
            }
            else
            {
                checkedListEmployee.Items.Clear();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close ();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            int position,length;
            this.m_Idcards.Clear();
            foreach (object checkItem in this.checkedListEmployee.CheckedItems)
            {
                position = checkItem.ToString().IndexOf('-');
                length = checkItem.ToString().Length;
                this.m_Idcards.Add(checkItem.ToString().Substring (position +1,length -position-1 ).ToString ());
            }
            this.Close();
        }
    }
}